package com.finaInorderProvider.config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.finaInorderProvider.entity.FinaAlipayBean;
import org.springframework.stereotype.Component;

/**
 * @author oahner
 * @create 2022-04-27 15:33
 */
@Component
public class FinaAliPay {

    /**
     * 支付接口
     * @param alipayBean
     * @return
     * @throws AlipayApiException
     */
    public static String pay(FinaAlipayBean alipayBean) throws AlipayApiException {
        FinaAlipayProperties.loadProperties();
        // 1、获得初始化的AlipayClient
        String serverUrl = FinaAlipayProperties.getGatewayUrl();
        String appId = FinaAlipayProperties.getAppId();
        String privateKey = FinaAlipayProperties.getPrivateKey();
        String format = "json";
        String charset = FinaAlipayProperties.getCharset();
        String alipayPublicKey = FinaAlipayProperties.getPublicKey();
        String signType = FinaAlipayProperties.getSignType();
        String returnUrl = FinaAlipayProperties.getReturnUrl();
        String notifyUrl = FinaAlipayProperties.getNotifyUrl();


        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);
        // 2、设置请求参数
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();
        // 页面跳转同步通知页面路径
        alipayTradePagePayRequest.setReturnUrl(returnUrl);
        // 服务器异步通知页面路径
        alipayTradePagePayRequest.setNotifyUrl(notifyUrl);
        // 封装参数
        alipayTradePagePayRequest.setBizContent(JSON.toJSONString(alipayBean));
        // 3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayTradePagePayRequest).getBody();
        // 返回付款信息
        return result;
    }


}
